package br.com.dreamsource.mobile.worders.forms.customer;

import br.com.dreamsource.mobile.components.DBChoiceGroup;
import br.com.dreamsource.mobile.jmesql.exceptions.SQLException;
import br.com.dreamsource.mobile.jmesql.jdbcResultSet;
import br.com.dreamsource.mobile.worders.WOrders;
import br.com.dreamsource.mobile.components.FormWOrders;
import br.com.dreamsource.mobile.worders.forms.core.Consts;
import br.com.dreamsource.mobile.worders.forms.core.DatabaseManager;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Elizeu Nogueira da Rosa Jr.
 * @version 0.1
 * @date 17.05.2007
 *
 */

public class CustomerList extends FormWOrders {
    
    private jdbcResultSet resultSet;
    
    private DBChoiceGroup radioGroupLocated;
    private String[] radioGroupLocatedItems;
    private Image[] radioGroupImage;
    
    public CustomerList(CommandListener listener, WOrders midlet, jdbcResultSet resultSet) {
        super(listener, midlet, "Lista de clientes");
        this.resultSet = resultSet;
        this.loadResultList(false);
    }
    
    private void loadResultList(boolean previous) {
        radioGroupLocated.deleteAll();
        if (previous) {
            try {
                int count = 0;
                while (resultSet.previous()) {
                    count++;
                    radioGroupLocated.append(resultSet.getString(Consts.CUSTOMER_ID), resultSet.getString(Consts.CUSTOMER_NAME), null);
                    if (count == 10) {
                        break;
                    }
                }
            } catch (SQLException sqlEx) {
                this.sendToForm(this.midlet.getSystemMessage("ERRO", sqlEx.getMessage(), this));
            }
        } else {
            try {
                while (resultSet.next()) {
                    radioGroupLocated.append(resultSet.getString(Consts.CUSTOMER_ID), resultSet.getString(Consts.CUSTOMER_NAME), null);
                }
            } catch (SQLException sqlEx) {
                this.sendToForm(this.midlet.getSystemMessage("ERRO", sqlEx.getMessage(), this));
            }
        }
        
    }

    private DBChoiceGroup getRadioGroupLocated() {
        if (radioGroupLocated == null) {
            
            radioGroupLocatedItems = new String[]{};
            radioGroupImage = new Image[]{};
            
            radioGroupLocated = new DBChoiceGroup("Resultados", Choice.EXCLUSIVE, 
                    radioGroupLocatedItems, radioGroupImage);
            radioGroupLocated.setSelectedFlags(new boolean[]{});
            radioGroupLocated.setFitPolicy(Choice.TEXT_WRAP_ON);
        }
        return radioGroupLocated;
    }       
    
    protected void createComponents() {
        this.append(this.getRadioGroupLocated());
        this.addCommand(this.getDeleteCommand());
        this.addCommand(this.getEditCommand());
        this.addCommand(this.getPreviousPageCommand());
        this.addCommand(this.getNextPageCommand());
        this.addCommand(this.getBackCommand());
        this.addCommand(this.getMainMenuCommand());
    }

    public void fireCommand(Command command) {
        try {
            if (command == nextPageCommand) {
                if (! resultSet.isAfterLast()) {
                    this.loadResultList(false);
                }
            } else
            if (command == previousPageCommand) {
                if (! resultSet.isBeforeFirst()) {
                    this.loadResultList(true);
                }
            } else
            if (command == editCommand) {
                DatabaseManager dbManager = new DatabaseManager(this.midlet.getUserPOJO());
                CustomerEdit customerEdit = this.midlet.getCustomerEdit(
                        Consts.UPDATE_TYPE_UPDATE, 
                        dbManager.findByField(
                            Consts.CUSTOMER_SELECT_BY_EDIT, 
                            Consts.CUSTOMER_ID, this.radioGroupLocated.getSelectedItemID()));
                customerEdit.putVariable(this.radioGroupLocated.getSelectedItemID());
                this.sendToForm(customerEdit);
                this.midlet.destroyForm(this);
            } else
            if (command == deleteCommand) {
                DatabaseManager dbManager = new DatabaseManager(this.midlet.getUserPOJO());
                CustomerEdit customerEdit = this.midlet.getCustomerEdit(
                        Consts.UPDATE_TYPE_DELETE, 
                        dbManager.findByField(
                            Consts.CUSTOMER_SELECT_BY_EDIT, 
                            Consts.CUSTOMER_ID, this.radioGroupLocated.getSelectedItemID()));
                customerEdit.putVariable(this.radioGroupLocated.getSelectedItemID());
                this.sendToForm(customerEdit);
                this.midlet.destroyForm(this);
            } else
            if (command == backCommand) {
                this.sendToForm(midlet.getCustomer());
                this.midlet.destroyForm(this);
            } else
            if (command == mainMenuCommand) {
                this.sendToForm(midlet.getMainMenu());
                this.midlet.destroyForm(this);
            }
        } catch (SQLException sqlEx) {
            this.sendToForm(this.midlet.getSystemMessage("ERRO", sqlEx.getMessage(), this));
        }
    }
    
}
